﻿// ***************************************************************************
// YAMM
// Copyright (C) 2008 Christian Schmiljun, Frank Weißenborn 
// http://www.codeplex.com/YAMM 
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// ***************************************************************************

using System;
using System.Collections;
using System.Collections.Generic;

using YAMM.PlugIn;

namespace YAMM.PlugInManagement.Windows
{
    class WindowCollection : IWindowCollection, IEnumerable
    {
        List<IWindow> windows;
        WindowManager owner;

        #region IEnumerable Member

        public IEnumerator GetEnumerator()
        {
            return windows.GetEnumerator();
        }

        #endregion


        #region IWindowCollection Member

        public IWindow Add(Guid identifier, string caption)
        {
            Window window = new Window(identifier, caption);
            window.Show(owner.DockPanel);
            window.Hide();
            return window;
        }

        public IWindow Add(Guid identifier, string caption, System.Windows.Forms.Control content)
        {
            Window window = new Window(identifier, caption, content);
            window.Show(owner.DockPanel);
            window.Hide();
            return window;
        }

        public bool Contains(Guid identifier)
        {
            bool retval = false;
            for (int i = 0; i < windows.Count; i++)
                if (identifier == windows[i].Identifier)
                {
                    retval = true;
                    break;
                }
            return retval;
        }

        public void Remove(Guid identifier)
        {
            try
            {
                Window item = (Window)this[identifier];
                item.Close();
                windows.Remove(item);
            }
            catch (ArgumentException)
            {
            }
        }

        public int Count
        {
            get { return windows.Count; }
        }

        public IWindow this[Guid identifier]
        {
            get
            {
                for (int i = 0; i < Count; i++)
                    if (this[i].Identifier == identifier)
                        return this[i];
                throw new ArgumentException("Kein IWindow mit diesem identifier gefunden.");
            }
        }

        #endregion   

        public WindowCollection(WindowManager owner)
        {
            this.owner = owner;
            this.windows = new List<IWindow>();
        }

        internal IWindow this[int index]
        {
            get
            {
                return windows[index];
            }
        }
    }
}
